## Pylons DB Admin
## Copyright (C) 2004-2009  Ken Kinder
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.

<%inherit file="base.mako"/>
<%def name="local_header()">
    <script type="text/javascript">
        var useFormat = ${h.json_dumps(c.useFormat)|n};
    </script>
    <script src="/CodeMirror-0.67/js/codemirror.js" type="text/javascript"></script>
    <script src="/js/console.js" type="text/javascript"></script>
</%def>

<%def name="title()">
    ${c.instance} SQL Console
</%def>

<div class="ui-corner-top ui-widget-header page-nav">
    <h1><a href="${h.url_for(controller='dbadmin', action='index')}">Pylons DB Admin</a> / ${c.instance} / SQL Console</h1>
</div>

<div id="console-top" class="ui-corner-all ui-widget-content">
    <form method="post">
        <table><tr valign="top" align="left"><td width="100%">
            <div class="sql-console-holder"><textarea name="statement" id="sql-console" class="sql-console">${c.statement}</textarea></div>
            <div>
            <div class="options-floater">
                    <label>Output Format: ${h.tags.select('format', c.format, [('auto', 'Automatic'), ('plain', 'Plain'), ('tabs', 'Tabs'), ('json', 'Data: JSON'), ('csv', 'Data: CSV')])}</label>
                </div>
                <button class="ui-button execute-button" type="submit">▶ Execute<br/>
                <span style="font-size: 7pt">(Ctrl+Enter)</span>
                </button>
                
            </div>
        </td><td>
            <ul id="table-list" class="table-list ui-state-default ui-corner-tl ui-corner-bottom">
                % for table in sorted(c.db.metadata.tables.keys()):
                    <li><a href="javascript:void(0);" name="${table}" class="table-link">${table}</a></li>
                    <ul class="column-list">
                    %  for column in c.db.metadata.tables[table].columns: 
                        <li><a href="javascript:void(0);" name="${column.name}" class="table-link">${column.name}</a></li>
                    % endfor
                    </ul>
                % endfor
            </ul>
        </td></tr></table>
    </form>
</div>
<br/>

% if c.results:
    <div id="result-tabs">
        % if len(c.results) > 1:
            % if c.useFormat == 'plain':
                <h3>Statements Executed</h3>
            % endif
            <ul>
                % for i, resultContainer in enumerate(c.results):
                    % if c.useFormat != 'errors-only' or resultContainer.isError():
                        <li><a href="#result-${i}">
                            % if resultContainer.isError():
                                <span style="color: red">&#9888; Error:</span>
                                ${h.text.truncate(unicode(resultContainer.statementParsed), length=22)}</a></li>
                            % else:
                                ${h.text.truncate(unicode(resultContainer.statementParsed), length=30)}</a></li>
                            % endif
                    % endif
                % endfor
            </ul>
        % endif
        % for i, resultContainer in enumerate(c.results):
            <div id="result-${i}" style="padding: 5px">
                % if c.useFormat == 'plain' or c.useFormat == 'errors-only' and resultContainer.isError():
                    <a name="result-${i}"><h3>${h.text.truncate(unicode(resultContainer.statementParsed), length=30)}</h3></a>
                % endif
                % if resultContainer.isError():
                    <div class="ui-widget">
                        <div style="padding: 0pt 0.7em;" class="ui-state-error ui-corner-all"> 
                            <p>&#9888; <strong>Alert:</strong> ${resultContainer.error}</p>
                        </div>
                    </div>
                % elif not c.useFormat == 'errors-only':
                    % if not (resultContainer.resultProxy.rowcount or resultContainer.resultProxy.lastrowid or resultContainer.data):
                        <div>${resultContainer.resultProxy.rowcount} rows affected.</div>
                    % else:
                        % if resultContainer.resultProxy.rowcount > 0:
                            <div>${resultContainer.resultProxy.rowcount} rows affected.</div>
                        % endif
                        % if resultContainer.resultProxy.lastrowid:
                            <div>Inserted row ID is '${resultContainer.resultProxy.lastrowid}'.</div>
                        % endif
                        % if not (resultContainer.resultProxy.rowcount > 0 or resultContainer.resultProxy.lastrowid):
                            <div>Statement executed successfully.</div>
                        % endif
                        % if resultContainer.data:
                            <table border="1" class="result-table" id="result-table-${i}">
                                % if getattr(resultContainer.resultProxy, 'keys', None):
                                    <tbody>
                                        <tr>
                                            % for k in resultContainer.resultProxy.keys:
                                                <th>${k}</th>
                                            % endfor
                                        </tr>
                                    </tbody>
                                % endif
                                <tbody>
                                    % for row in resultContainer.data:
                                        <tr>
                                            % for cell in row:
                                                <td>${cell}</td>
                                            % endfor
                                        </tr>
                                    % endfor
                                </tbody>
                            </table>
                        % endif
                    % endif
                % endif
            </div>
        % endfor
% endif
